﻿using Microsoft.EntityFrameworkCore;
using PHMEE.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;

namespace PHMEE
{
    public class SysParaRepository : EfCoreRepository<PHMEEDbContext, SysPara, Guid>, ISysParaRepository
    {
        public SysParaRepository(IDbContextProvider<PHMEEDbContext> dbContextProvider) : base(dbContextProvider)
        {
        }
        /// <summary>
        /// 更新数采器状态
        /// </summary>
        /// <param name="sn">采集器编号</param>
        /// <returns></returns>
        public async Task UpdataOnlineStatueAsync(string sn, bool isOnline)
        {
            var dbContext = await GetDbContextAsync();
            var sysPara = await dbContext.SysParas.FirstOrDefaultAsync(x => x.CollectorCode == sn);
            if (sysPara != null && sysPara.IsOnline != isOnline)
            {
                //更新数采器状态
                sysPara.IsOnline = isOnline;
                dbContext.SysParas.Update(sysPara);
                await dbContext.SaveChangesAsync();
            }
        }
        /// <summary>
        /// 在线数量
        /// </summary>
        /// <returns></returns>
        public async Task<long> GetOnlineCountAsync()
        {
            var dbContext = await GetDbContextAsync();
            return await dbContext.SysParas.LongCountAsync(x => x.IsOnline);
        }
        /// <summary>
        /// 设置下发参数时间
        /// </summary>
        /// <param name="sn"></param>
        /// <returns></returns>
        public async Task SetParaTimeAsync(string sn)
        {
            var dbContext = await GetDbContextAsync();
            var sysPara = await dbContext.SysParas.FirstOrDefaultAsync(x => x.CollectorCode == sn);
            if (sysPara != null)
            {
                if (sysPara.ParaTime == null)
                {
                    sysPara.ParaTime = DateTime.UtcNow;
                    dbContext.SysParas.Update(sysPara);
                    await dbContext.SaveChangesAsync();
                }
            }
        }
        /// <summary>
        /// 获取所有采集站编码 (CollectCode)
        /// </summary>
        /// <returns>采集站编码的列表</returns>
        public async Task<List<string>> GetAllCollectCodesAsync()
        {
            var dbContext = await GetDbContextAsync();
            return await dbContext.SysParas
                .Select(x => x.CollectorCode)
                .Distinct()
                .ToListAsync();
        }

        /// <summary>
        /// 根据采集器编码获取 SysPara
        /// </summary>
        /// <param name="collectorCode">采集器编码</param>
        /// <returns></returns>
        public async Task<SysPara> GetSysParaByCollectorCodeAsync(string collectorCode)
        {
            var dbContext = await GetDbContextAsync();
            return await dbContext.SysParas
                .FirstOrDefaultAsync(sp => sp.CollectorCode == collectorCode);
        }
    }
}
